package com.example.mapreducr.writable;

/**
 * Created with IntelliJ IDEA.
 * ClassName: FlowBean
 * Package: com.example.mapreducr.writable
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-15
 * Time: 21:33
 */

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 1.自定义序列化对象
 * 2.实现writable接口
 * 3.重写序列化和反序列化接口
 * 4.写空参构造 反序列化要调用
 * 重点:序列化对象的顺序和反序列化顺序要一致
 * 5.如果序列化值最为k传递 重写比较Comparable接口
 * 6.想把结果展示在文件中 重写toString方法
 */
public class FlowBean implements Writable {

    //需求是统计每个电话好嘛 对应的上下行流量 和 总流量
    //输出的k值是电话好嘛 v是 三中流量 封装为一个对象
    private Long up;
    private Long down;
    private Long sum;

    public Long getUp() {
        return up;
    }

    public void setUp(Long up) {
        this.up = up;
    }

    public Long getDown() {
        return down;
    }

    public void setDown(Long down) {
        this.down = down;
    }

    public Long getSum() {
        return sum;
    }

    public void setSum(Long sum) {
        this.sum = sum;
    }

    public void setSum() {
        this.sum = this.up + this.down;
    }

    //重写空参构造
    public FlowBean() {
    }

    //序列化 和 返序列化要保持一致
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(up);
        dataOutput.writeLong(down);
        dataOutput.writeLong(sum);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.up = dataInput.readLong();
        this.down = dataInput.readLong();
        this.sum = dataInput.readLong();
    }

    @Override
    public String toString() {
        return up + "\t" + down + "\t" + sum;
    }
}
